﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Windows.Browser;
namespace HtmlExtractor
{
    internal static class StringExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string EscapeXML(this string s)
        {
            if (string.IsNullOrEmpty(s)) return s;
            
            string t = s.Replace("<", "&lt;").Replace(">", "&gt;").Replace("'", "&apos;").Replace("&", "&amp;").Replace("\"", "&quot;");
            return t;
            //if (string.IsNullOrEmpty(s)) return s;
            //return !SecurityElement.IsValidText(s)
            //       ? SecurityElement.Escape(s) : s;

        }
        /// <summary>
        /// 替换所有显示不需要的字符
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string FormatXml(this string s)
        {
            s = s.Replace("&nbsp;", " ");
            s = s.EscapeXML();
           return s = System.Text.RegularExpressions.Regex.Replace(s, @"[\x00-\x08]|[\x0B-\x0C]|[\x0E-\x1F]", "");
          // return s.Replace("&nbsp;", "");
        }
        public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
        {
            return string.Format(provider, format, args);
        }
        public static string UnescapeXML(this string s)
        {

            if (string.IsNullOrEmpty(s)) return s;
            string returnString = s;
            returnString = returnString.Replace("&apos;", "'");
            returnString = returnString.Replace("&quot;", "\"");
            returnString = returnString.Replace("&gt;", ">");
            returnString = returnString.Replace("&lt;", "<");
            returnString = returnString.Replace("&amp;", "&");
            return returnString;

        }
        public static RegexOptions ToRegexOptions(this string options)
        {
           RegexOptions regeOpt= options==null?RegexOptions.None:(RegexOptions)Enum.Parse(typeof(RegexOptions), options,true);
            return regeOpt;
        }
        public static string GetChildAttribute(this XElement element, string childElement, string attibuteName)
        {
            return element.Element(childElement) == null ? null : (string)element.Element(childElement).Attribute(attibuteName);
        }

        public static string GetRequriedChildAttribute(this XElement element, string childElement, string attibuteName)
        {
            XElement e = element.Element(childElement);
            if (e != null)
            {
                string attr = (string)e.Attribute(attibuteName);
                Assert.IsNullOrEmpty(attr);
                return attr;
            }
            else
            {
                throw new ConfigException("不存在元素" + childElement);
            }

        }
        public static string GetRequierdAttribute(this XElement element, string attibuteName)
        {
            string attr = (string)element.Attribute(attibuteName);
            Assert.IsNullOrEmpty(attr);
            return attr;
        }

    }
}


